package com.woniuxy.mqConsumer.service;

import com.woniuxy.mqConsumer.entity.Orders;
import com.woniuxy.mqConsumer.entity.Product;
import com.woniuxy.mqConsumer.entity.Room;
import com.woniuxy.mqConsumer.entity.RoomUser;
import com.woniuxy.mqConsumer.mapper.OrderMapper;
import com.woniuxy.mqConsumer.mapper.ProductMapper;
import com.woniuxy.mqConsumer.mapper.RoomMapper;
import com.woniuxy.mqConsumer.mapper.RoomUserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.util.Date;
import java.util.List;

@Service
public class RoomService {
    @Autowired
    RoomMapper roomMapper;
    @Autowired
    ProductMapper productMapper;
    @Autowired
    RoomUserMapper roomUserMapper;
    @Autowired
    OrderMapper orderMapper;
    @Autowired
    RestTemplate restTemplate;
    public void startRoomStatus(Room room) {
        //先将房间状态改为正在竞拍
        room.setStatus(1);
        roomMapper.updateByPrimaryKey(room);
        //再进入竞拍倒计时，发送MQ消息(利用openfeign)
        String url = "http://localhost:11111/room/countdown?roomId="+room.getId();
        restTemplate.getForObject(url, Void.class);
    }

    public void endRoomStatus(Room room) {
        //先将房间状态改为已结束
        Room endRoom = roomMapper.selectByPrimaryKey(room.getId());
        endRoom.setStatus(2);
        roomMapper.updateByPrimaryKey(endRoom);
        //查看当前拍卖的商品信息和该房间的中间表信息
        Product product = productMapper.selectByPrimaryKey(endRoom.getProductid());
        List<RoomUser> roomUser = roomUserMapper.selectByRoomId(endRoom.getId());
        //再查看是否有人竞拍
        if(endRoom.getCurrprice() != null){
            Integer endUserId = endRoom.getOwnerid();
            //有人竞拍则生成订单,订单状态为待支付
            Orders orders = new Orders();
            orders.setProductid(product.getId());
            orders.setBuyuserid(endUserId);
            orders.setOrderstatus(0);
            orders.setCreatdate(new Date());
            orders.setTotalprice(endRoom.getCurrprice());
            orders.setSaleuserid(product.getUserid());
            orders.setRoomid(endRoom.getId());
            orderMapper.insert(orders);
            //给其他用户退保证金
            for (RoomUser user : roomUser) {
                if(user.getUserid() != endUserId){
                    /**
                     * 没有主键无法使用tkmapper
                     */
                    user.setStatus(1);
                    roomUserMapper.updateByPrimaryKey(user);
                }
            }
            //进入订单支付倒计时，发送MQ消息(利用openfeign)
            System.err.println("发送短信");
            String url = "http://localhost:11111/orders/sendMessage?ordersId="+orders.getId();
            restTemplate.getForObject(url, Void.class);
        }else {
            //无人竞拍则将房间状态修改为流拍，修改商品状态为未成交
            endRoom.setStatus(3);
            roomMapper.updateByPrimaryKey(endRoom);
            product.setStatus(0);
            productMapper.updateByPrimaryKey(product);
            //查看中间表是否有人缴纳保证金，有则退回
            if(roomUser != null && roomUser.size() != 0){
                //退回保证金并修改中间表状态为保证金已退还(是否设置退款账户)
                for (RoomUser user : roomUser) {
                    /**
                     * 没有主键无法使用tkmapper
                     */
                    user.setStatus(1);
                    roomUserMapper.updateByPrimaryKey(user);
                }
                System.err.println("流拍");
            }
        }
    }


}
